var graph = argument0;
var cell = argument1;
var dir = argument2;

//int length = 0;
var length  = 0;

//int height = graph.GetLength(0);
var height = array_height_2d(graph);
//int width = graph.GetLength(1);
var width  = array_length_2d(graph, 0);
if (dir == 0)
{
    //int[] next = new int[] {cell[0], cell[1] + 1};
    var next = 0;
    next[0] = cell[0];
    next[1] = cell[1] + 1;
    while (next[1] < width && graph[next[0], next[1]] != 0)
    {
        length++;
        next[1]++;
    }
}
if (dir == 1)
{
    //int[] next = new int[] { cell[0] - 1, cell[1] };
    var next = 0;
    next[0] = cell[0] - 1;
    next[1] = cell[1];
    while (next[0] > -1 && graph[next[0], next[1]] != 0)
    {
        length++;
        next[0]--;
    }
}
if (dir == 2)
{
    //int[] next = new int[] { cell[0], cell[1] - 1 };
    var next = 0;
    next[0] = cell[0];
    next[1] = cell[1] - 1;
    while (next[1] > -1 && graph[next[0], next[1]] != 0)
    {
        length++;
        next[1]--;
    }
}
if (dir == 3)
{
    //int[] next = new int[] { cell[0] + 1, cell[1] };
    var next = 0;
    next[0] = cell[0] + 1;
    next[1] = cell[1];
    while (next[0] < height && graph[next[0], next[1]] != 0)
    {
        length++;
        next[0]++;
    }
}
return length;